package org.example.platform.elasticsearch.controller;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.example.platform.elasticsearch.entity.Hotel;
import org.example.platform.elasticsearch.service.EsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
public class TestController {
    @Autowired
    private EsService esService;

    @PostMapping("/test")
    public String getResult(String keyword) {
        try {
            List<Hotel> hotelList = esService.getHotelFromTitle(keyword);
            if (CollectionUtil.isNotEmpty(hotelList)) {
                return hotelList.toString();
            } else {
                return "no data";
            }
        } catch (IOException e) {
            log.warn("搜索发生异常，原因为:{}", e.getMessage());
            return 100+ e.getMessage();
        } catch (Exception e) {
            log.error("服务发生异常，原因为:{}", e.getMessage());
            return 100+e.getMessage();
        }

    }
}
